﻿using System;
using System.Text;
using RAP.Framework.Libary.DomTemplateBuilder.Element;
using RAP.Framework.Libary.Utils;
using RAP.Framework.Libary.DomTemplateBuilder.Interface;

namespace RAP.Framework.Libary.DomTemplateBuilder.Handler
{
	/// <summary>
	/// 表达式处理器基类，将C#表达式中的and、or转义
	/// </summary>
	public class ExpressionHandler : DynamicHandler
    {
        public override void DoRender ( StringBuilder writer, object rootParamObject, object parentParamObject, ITemplateRenderContext context )
        {
            var ele = this.Element as Expression;
			var sb = new StringBuilder();
            base.DoRender( sb, rootParamObject, parentParamObject, context );
			var content = ExpressionUtils.ParseCSharpExpression( sb.ToString() );
            if ( content.IsNotNullOrEmpty() )
            {
                writer.Append( content );
            }
            else if ( !ele.IgnoreEmpty )
            {
                throw new NullReferenceException( "表达式不能为空！如果想忽略该错误，请将Expression节点的IgnoreEmpty设置为true。" );
            }
        }
    }
};